package org.myorg.v5;

public class Fmap extends Fc5 {
	int goals_num = 3;
	
	//double p[][] = { {500d, 200, 10}, {100, 500, 50}, {50,  50,  100}};
	//double v[][] = { {400, 100, 10}, {80, 500, 40}, {45,50,90}};
	
	double p[][] = { {1000d, 4000d, 100d},
			{500d, 3000d, 10d},
			{2000d, 2000d, 50d},
			{200d, 500d, 20d},
			{600d, 600d, 50d},
			{700d, 1000d,100d} 
	};
	
	double v[][] = { 
			{400d, 100d, 10d},
			{200d, 200d, 20d},
			{500d, 600d, 10d},
			{1000d,200d, 30d},
			{400d, 800d, 30d},
			{70d, 20d, 5d},
			{1500d, 2000d, 10d},
			{500d, 1000d, 60d},
			{60d, 80d, 100d},
			{700d, 80d, 50d}
			
	};
	
	int n = p.length;
	int m = v.length;



	public Fmap() {
		print();
	}
	public Fmap(int n, int m) {
		this.n = n;
		this.m = m;
		
		p = new double[n][3];
		v = new double[m][3];
		
		for (int i = 0; i < n; i++ ) {
			p[i][0] = Math.random()*(2000-100) + 100;
			p[i][1] = Math.random()*(4000-10) + 10;
			p[i][2] = Math.random()*(100-1) + 1;
		}
		for (int i = 0; i < m; i++ ) {
			v[i][0] = Math.random()*(2000-100) + 100;
			v[i][1] = Math.random()*(4000-10) + 10;
			v[i][2] = Math.random()*(100-1) + 1;
		}
		//print();
		System.out.println("goals_num: " + goals_num);
		System.out.println("n: " + n);
		System.out.println("m:" + m);
		
	}
	public Fmap(String paper) {
		this.n = 6;
		this.m = 12;
		
		//p = { {500d, 200, 10}, {100, 500, 50}, {50,  50,  100}};
		
	}

	protected void print() {
		System.out.println("goals_num: " + goals_num);
		System.out.println("n: " + n);
		System.out.println("m:" + m);
		System.out.println("array p: ");
		for (double[] x:p) {
			for(double y:x) {
				System.out.print(y + " ");
			}
			System.out.println();
		}
		System.out.println("array v: ");
		for (double[] x:v) {
			for(double y:x) {
				System.out.print(y + " ");
			}
			System.out.println();
		}
	}
	
	// added in 2011-10-15
	public double[][] cal_resource(FGene5 ps,int goal) {
		double[][] all = new double[n+1][2];
		
		
		for (int i = 0; i < n; i++) {
			System.out.print(p[i][goal]);
		}
		
		System.out.println();
		
		
		double ptotal = 0;
		double vtotal = 0;
		
		for (int i = 0; i < n; i++) {
			ptotal += p[i][goal]; 
		}
		
		for (int i = 0; i < m; i++) {
			vtotal += v[i][goal];
		}
		
		double rtotal = vtotal/ptotal;
		
		all[n][0] = vtotal;
		all[n][1] = rtotal;
		
		for (int i=0; i<n; i++) {
			double vi = 0;
			
			for (int j=0; j<m; j++) {
				String tmp =  Integer.toString(i);
				while (tmp.length() < ps.sl) {
					tmp = "0" + tmp;
				}
				//if (ps.gene.charAt(j) == Integer.toString(i).charAt(0)) {
				//	vi += v[j][goal];
				//}
				String test = ps.gene.substring(j*ps.sl,(j+1)*ps.sl);
				if (ps.gene.substring(j*ps.sl,(j+1)*ps.sl).equals(tmp)) {
					vi += v[j][goal];
				}
			}
			
			all[i][0] = vi; 
			all[i][1] = vi/p[i][goal];
		}
		
		
		
		
		
		return all;
	}
	
	protected double calculate_obj(FGene5 ps,int goal) {
		double obj_v = 0;
		
		double ptotal = 0;
		double vtotal = 0;
		
		for (int i = 0; i < n; i++) {
			ptotal += p[i][goal]; 
		}
		
		for (int i = 0; i < m; i++) {
			vtotal += v[i][goal];
		}
		
		double rtotal = vtotal/ptotal;
		
		double[] r = new double[n];
		
		for (int i=0; i<n; i++) {
			double vi = 0;
			
			for (int j=0; j<m; j++) {
				String tmp =  Integer.toString(i);
				while (tmp.length() < ps.sl) {
					tmp = "0" + tmp;
				}
				//if (ps.gene.charAt(j) == Integer.toString(i).charAt(0)) {
				//	vi += v[j][goal];
				//}
				String test = ps.gene.substring(j*ps.sl,(j+1)*ps.sl);
				if (ps.gene.substring(j*ps.sl,(j+1)*ps.sl).equals(tmp)) {
					vi += v[j][goal];
				}
			}
			r[i] = vi/p[i][goal];
		}
		
		double mid_v = 0;
		for (int i=0; i<n; i++) {
			mid_v += (r[i] - rtotal) * (r[i] - rtotal);
		}
		
		obj_v = Math.pow(mid_v, 1d/2d);
		
		return obj_v;
	}
	
	@Override
	public double[] org_value(FGene5 ps) {
		
		double[] obj = new double[goals_num];
		
		for (int i=0; i<3; i++) {
			obj[i] = calculate_obj(ps,i);
		}
		
		return obj;
	}

	public int getGoals_num() {
		return goals_num;
	}
	public double[][] getP() {
		return p;
	}
	public double[][] getV() {
		return v;
	}
	public int getN() {
		return n;
	}
	public int getM() {
		return m;
	}
	@Override
	public int get_goals_num() {
		return goals_num;
	}

}
